import { Component, Injectable } from '@angular/core';
import { NavController, NavParams, ToastController, AlertController, App } from 'ionic-angular';
import { Api } from "../providers/api/api";
import { ListMasterPage } from "../pages/list-master/list-master";
import { HostListener } from "@angular/compiler/src/core";
import { QueInfo, RowInfo, ColumnInfo, FType, DropdownInfo, TopicResultInfo, ListChildInfo, AnswerLog, QueOutput, AnswerLogUp, MobileAnswer, MobileAnserChd } from '../models/quesinfo';
import { IonicStorageModule, Storage } from '@ionic/storage';
import { SQLite, SQLiteObject } from '@ionic-native/sqlite';
import { Device } from '@ionic-native/device';
@Injectable()
export class localbase {
    db: SQLiteObject;
    constructor(
        private device: Device,
        private sqlite: SQLite,
    ) {
        if (this.device.cordova !== null) {
            this.sqlite
                .create({ name: 'data.db', location: 'default' })
                .then((db: SQLiteObject) => {
                    // 问卷表
                    db.executeSql(`CREATE TABLE  IF NOT EXISTS Ques(
                    fqid NVARCHAR(60) PRIMARY KEY NOT NULL,
                    fbsj NVARCHAR(20) NOT NULL,
                    body TEXT NOT NULL)`, {}).then(() => console.log('Executed SQL'))
                        .catch(e => console.log(e));
                    // 问卷表
                    db.executeSql(`CREATE TABLE  IF NOT EXISTS OnServerQues(
                        fqid NVARCHAR(60) PRIMARY KEY NOT NULL,
                        fbsj NVARCHAR(20) NOT NULL,
                        body TEXT NOT NULL)`, {}).then(() => console.log('Executed SQL'))
                        .catch(e => console.log(e));
                    // 问卷日志表
                    db.executeSql(`CREATE TABLE  IF NOT EXISTS QuesLog(
                    fqid NVARCHAR(60) NOT NULL,
                    fbsj NVARCHAR(20) NOT NULL,
                    body TEXT NOT NULL)`, {})
                        .then(() => console.log('Executed SQL'))
                        .catch(e => console.log(e));
                    // 作答记录表
                    db.executeSql(`CREATE TABLE  IF NOT EXISTS QuesAnswer(
                    fqid NVARCHAR(60) NOT NULL,
                    fbsj NVARCHAR(20) NOT NULL,
                    answerId NVARCHAR(60),
                    FUpdateloadState INT,
                    body TEXT NOT NULL)`, {}).then(() => console.log('Executed SQL'))
                        .catch(e => console.log(e));
                    this.db = db;
                })
                .catch(e => console.log(e));
        }
    }

    ngOnInit(): void {

    }

    //获取是否已下载
    async isDown(FQId) {
        let isDown = false;
        if (this.db) {
            const value = await this.db.executeSql(`select COUNT(fqid) from Ques where fqid=?`, [FQId]);
            return value.rows.item(0)['COUNT(fqid)'] > 0;
        } else {
            let QueMenuLast = localStorage.getItem("QueMenuLast");
            if (QueMenuLast != undefined && QueMenuLast != "" && QueMenuLast != null) {
                let QueMenuLastArr = QueMenuLast.split('.');
                QueMenuLastArr.forEach(e => {
                    let thisFQId = e.split(',')[0];
                    if (thisFQId == FQId) {
                        isDown = true;
                    }
                });
            }
        }
        return isDown;
    }

    //下载保存问卷
    async addQueDownLoad(FQId, thisque) {
        if (this.db) {
            if (this.isDown(FQId)) {
                // 插入日志数据，删除问卷表信息。
                await this.db.executeSql(`
                         delete from Ques where fqid=?`, [FQId]).catch(e => { console.log(`addQueDownLoad删除问卷表信息`); console.log(e); });;
            }
            // console.log(thisque);
            // 插入新的问卷信息
            const queInfo = JSON.stringify(thisque);
            await this.db.executeSql(`
            INSERT INTO Ques( fqid, fbsj, body)
            VALUES ( ?,?,?)`, [FQId, thisque.Que.FPublishTime, queInfo]).catch(e => { console.log(`addQueDownLoad插入新的问卷信息`); console.log(e); });
            await this.db.executeSql(`
                        INSERT INTO QuesLog( fqid, fbsj, body)
                        select fqid, fbsj, body from Ques where fqid=?`, [FQId]).catch(e => { console.log(`addQueDownLoad插入日志数据`); console.log(e); });
        } else {
            //新增最新问卷记录
            let QueMenuLast = localStorage.getItem("QueMenuLast");//最新问卷的目录
            let addQueMenuLast = FQId + "," + thisque.Que.FPublishTime;
            if (QueMenuLast == undefined || QueMenuLast == "" || QueMenuLast == null) {
                QueMenuLast = addQueMenuLast;
            }
            else {
                QueMenuLast += "." + addQueMenuLast;
            }
            localStorage.setItem("QueLast_" + addQueMenuLast, JSON.stringify(thisque));
            localStorage.setItem("QueMenuLast", QueMenuLast);
            //新增问卷过往记录
            let QueMenuLog = localStorage.getItem("QueMenuLog");//最新问卷的目录
            let addQueMenuLog = FQId + "," + thisque.Que.FPublishTime;
            if (QueMenuLog == undefined || QueMenuLog == "" || QueMenuLog == null) {
                QueMenuLog = addQueMenuLog;
            }
            else {
                QueMenuLog += "." + addQueMenuLog;
            }
            localStorage.setItem("QueLog_" + addQueMenuLog, JSON.stringify(thisque));
            localStorage.setItem("QueMenuLog", QueMenuLog);
        }
    }

    //获取问卷列表
    async getQuesList(): Promise<Array<any>> {
        let result = new Array<any>();
        if (this.db) {
            const value = await this.db.executeSql(`
                        select body from Ques`, {}).catch(e => { console.log(`addQueDownLoad插入日志数据`); console.log(e); });
            //  console.log('问卷列表数据库数据');
            //  console.log(value);
            if (value.rows.length) {
                for (let index = 0; index < value.rows.length; index++) {
                    result.push(JSON.parse(value.rows.item(index)['body']).Que);
                    // console.log(index);
                }
            }
        } else {
            //新增最新问卷记录
            result = JSON.parse(localStorage.getItem("localQuestion"));
        }
        console.log('获取本地问卷列表');
        console.log(result);
        return result;
    }

    //获取服务器问卷列表
    async getServerQuesList(): Promise<Array<any>> {
        let result = new Array<any>();
        if (this.db) {
            const value = await this.db.executeSql(`
                        select body from OnServerQues`, {}).catch(e => { console.log(`获取服务器问卷列表`); console.log(e); });
            if (value.rows.length) {
                for (let index = 0; index < value.rows.length; index++) {
                    result.push(JSON.parse(value.rows.item(index)['body']));
                }
            }
        } else {
            //新增最新问卷记录
            result = JSON.parse(localStorage.getItem("serverQues"));
        }
        return result;
    }
    // 添加未下载问卷信息
    async addServerQues(que: Array<any>) {
        if (que) {
            if (this.db) {
                await this.db.executeSql(`delete from OnServerQues`, {}).catch(e => { console.log(`添加未下载问卷信息:delete`); console.log(e); });
                que.forEach(async q => {
                    await this.db.executeSql(`
                    INSERT INTO OnServerQues( fqid, fbsj, body)
                    VALUES ( ?,?,?)`, [q.FQId, q.FPublishTime, JSON.stringify(q)]).catch(e => { console.log(`添加未下载问卷信息`); console.log(e); });
                });
            } else {
                //新增最新问卷记录
                localStorage.setItem("serverQues", JSON.stringify(que));
            }
        }
    }

    // 删除未下载问卷信息
    async delServerQues(fqid) {
        if (fqid) {
            if (this.db) {
                await this.db.executeSql(`
                    delete from OnServerQues where  fqid=?`, [fqid]).catch(e => { console.log(`删除未下载问卷信息`); console.log(e); });
            } else {
                let result = JSON.parse(localStorage.getItem("serverQues"));
                if (result) {
                    result = result.filter(e => e.FQId !== 'fqid');
                    //新增最新问卷记录
                    localStorage.setItem("serverQues", JSON.stringify(result));
                }

            }
        }
    }

    //获取问卷结构去作答
    async getQueByFqid(FQId): Promise<any> {
        let result = null;
        if (this.db) {
            const value = await this.db.executeSql(`select body from Ques where fqid=?`, [FQId]).catch(e => { console.log(`getQueByFqid：`); console.log(e); });
            if (value.rows.length) {
                result = JSON.parse(value.rows.item(0)['body']);
            }
        } else {
            let QueMenuLast = localStorage.getItem("QueMenuLast");
            if (QueMenuLast != undefined && QueMenuLast != "" && QueMenuLast != null) {
                let QueMenuLastArr = QueMenuLast.split('.');
                QueMenuLastArr.forEach(e => {
                    let thisFQId = e.split(',')[0];
                    if (thisFQId == FQId) {
                        result = JSON.parse(localStorage.getItem("QueLast_" + e));
                    }
                });
            }
        }
        return result;
    }
    //更新问卷
    async editQue(thisQueMenuOne, thisque) {
        if (this.db) {
            // console.log('更新问卷');
            // console.log(thisque);
            // 插入日志数据，删除问卷表信息。
            await this.db.executeSql(`
                        delete from Ques where fqid=?`, [thisque.Que.FQId]).catch(e => { console.log(`editQue插入日志数据，删除问卷表信息。`); console.log(e); });
            // 插入新的问卷信息
            await this.db.executeSql(`
            INSERT INTO Ques( fqid, fbsj, body)
            VALUES ( ?,?,?)`, [thisque.Que.FQId, thisque.Que.FPublishTime, JSON.stringify(thisque)]).catch(e => { console.log(`editQue插入新的问卷信息`); console.log(e); });
            await this.db.executeSql(`
            INSERT INTO QuesLog( fqid, fbsj, body)
            select fqid, fbsj, body from Ques where fqid=?`, [thisque.Que.FQId]).catch(e => { console.log(`editQue  QuesLog`); console.log(e); });
        } else {
            localStorage.setItem("QueLast_" + thisQueMenuOne, JSON.stringify(thisque));
            //新增问卷过往记录
            let QueMenuLog = localStorage.getItem("QueMenuLog");//最新问卷的目录
            let addQueMenuLog = thisQueMenuOne;
            if (QueMenuLog == undefined || QueMenuLog == "" || QueMenuLog == null) {
                QueMenuLog = addQueMenuLog;
            }
            else {
                QueMenuLog += "." + addQueMenuLog;
            }
            localStorage.setItem("QueLog_" + addQueMenuLog, JSON.stringify(thisque));
            localStorage.setItem("QueMenuLog", QueMenuLog);
        }
    }
    //删除问卷
    async delQue(FQId) {
        if (this.db) {
            if (this.isDown(FQId)) {
                // 插入日志数据，删除问卷表信息。
                await this.db.executeSql(`
                        delete from QuesLog where fqid=?`, [FQId]).catch(e => { console.log(`delQue删除日志`); console.log(e); });
                await this.db.executeSql(`
                        delete from Ques where fqid=?`, [FQId]).catch(e => { console.log(`delQue删除问卷`); console.log(e); });
            }
        } else {
            //删除最新问卷
            let QueMenuLast = localStorage.getItem("QueMenuLast");
            let newMenuLast = "";
            if (QueMenuLast != undefined && QueMenuLast != "" && QueMenuLast != null) {
                let QueMenuLastArr = QueMenuLast.split('.');
                QueMenuLastArr.forEach(e => {
                    let thisQueFqid = e.split(',')[0];
                    if (FQId != thisQueFqid) {
                        newMenuLast += e + ".";
                    }
                    else {
                        localStorage.removeItem("QueLast_" + e);
                    }
                })
            }
            localStorage.setItem("QueMenuLast", newMenuLast);
            //删除问卷记录
            let QueMenuLog = localStorage.getItem("QueMenuLog");
            let newQueMenuLog = "";
            if (QueMenuLog != undefined && QueMenuLog != "" && QueMenuLog != null) {
                let QueMenuLogArr = QueMenuLog.split('.');
                QueMenuLogArr.forEach(e => {
                    let thisQueFqid = e.split(',')[0];
                    if (FQId != thisQueFqid) {
                        newQueMenuLog += e + ".";
                    }
                    else {
                        localStorage.removeItem("QueLog_" + e);
                    }
                })
            }
            localStorage.setItem("QueMenuLog", newQueMenuLog);
        }
    }
    //获取问卷结构去编辑
    async getQueByFqidEdit(answerid): Promise<any> {
        let answerlog = await this.getbaseAnswerlogByanswerid(answerid);
        // console.log('answerlog');
        // console.log(answerlog);
        let result = null;
        if (this.db) {
            // 插入新的问卷信息
            const value = await this.db.executeSql(`select body from QuesLog where fqid=?`, [answerlog.FQId]);
            // console.log('插入新的问卷信息');
            // console.log(value);
            if (value.rows.length) {
                result = JSON.parse(value.rows.item(0)['body']);
            }
        } else {
            let QueMenuLog = localStorage.getItem("QueMenuLog");
            if (QueMenuLog != undefined && QueMenuLog != "" && QueMenuLog != null) {
                let QueMenuLogArr = QueMenuLog.split('.');
                QueMenuLogArr.forEach(e => {
                    let thisFQId = e.split(',')[0];
                    if (e == (answerlog.FQId + "," + answerlog.FPublishTime)) {
                        result = JSON.parse(localStorage.getItem("QueLog_" + e));
                    }
                });
            }
        }
        return result;
    }

    /**
     * 保存作答记录
     * @param answerid 
     * @param FQId 
     * @param FPublishTime 
     * @param oneAnswerLog 
     */
    async setbaseAnswerlog(answerid, FQId, FPublishTime, oneAnswerLog) {
        if (this.db) {
            const value = await this.db.executeSql(`
            INSERT INTO QuesAnswer( fqid, fbsj,answerId, body)
            VALUES ( ?,?,?,?);`, [FQId, FPublishTime, answerid, JSON.stringify(oneAnswerLog)]).catch(e => console.log(e));
        } else {
            let AnswerMenu = localStorage.getItem("AnswerMenu");
            let addAnswerMenu = answerid + "," + FQId + "," + FPublishTime;
            if (AnswerMenu == undefined || AnswerMenu == "" || AnswerMenu == null) {
                AnswerMenu = addAnswerMenu;
            }
            else {
                AnswerMenu += "." + addAnswerMenu;
            }
            localStorage.setItem("Answer_" + addAnswerMenu, JSON.stringify(oneAnswerLog));
            localStorage.setItem("AnswerMenu", AnswerMenu);
        }
    }

    /**
     * 更新作答记录
     * @param answerid 
     * @param FQId 
     * @param FPublishTime 
     * @param oneAnswerLog 
     */
    async setbaseAnswerlogGoon(answerid, FQId, FPublishTime, oneAnswerLog) {
        if (this.db) {
            const value = await this.db.executeSql(`
            UPDATE QuesAnswer set body=?, FUpdateloadState=? where answerId=?`,
                [JSON.stringify(oneAnswerLog), oneAnswerLog.FUpdateloadState, answerid]);
        } else {
            let addAnswerMenu = answerid + "," + FQId + "," + FPublishTime;
            localStorage.setItem("Answer_" + addAnswerMenu, JSON.stringify(oneAnswerLog));
        }
    }
    /**
     * 获取作答记录
     * @param answerid 
     */
    async getbaseAnswerlogByanswerid(answerid): Promise<any> {
        let result = null;
        if (this.db) {
            // 插入新的问卷信息
            const value = await this.db.executeSql(`select body from QuesAnswer where answerId=?`, [answerid]);
            if (value.rows.length) {
                result = JSON.parse(value.rows.item(0)['body']);
            }
        } else {
            let AnswerMenu = localStorage.getItem("AnswerMenu");
            if (AnswerMenu != undefined && AnswerMenu != "" && AnswerMenu != null) {
                let AnswerMenuArr = AnswerMenu.split('.');
                AnswerMenuArr.forEach(e => {
                    let thisanswerid = e.split(',')[0];
                    if (thisanswerid == answerid) {
                        result = JSON.parse(localStorage.getItem("Answer_" + e));
                    }
                });
            }
        }
        return result;
    }

    /**
     * 根据问卷主键，获取所有作答记录。
     * @param FQId 
     */
    async getbaseAnswerlogByFQId(FQId) {
        let answerlogs = new Array<AnswerLog>();
        if (this.db) {
            // 插入新的问卷信息
            const value = await this.db.executeSql(`select body from QuesAnswer where fqid=?`, [FQId]);
            for (let i = 0; i < value.rows.length; i++) {
                answerlogs.push(JSON.parse(value.rows.item(i)['body']));
            }
        } else {
            let AnswerMenu = localStorage.getItem("AnswerMenu");
            if (AnswerMenu != undefined && AnswerMenu != "" && AnswerMenu != null) {
                let AnswerMenuArr = AnswerMenu.split('.');
                AnswerMenuArr.forEach(e => {
                    let thisFQId = e.split(',')[1];
                    if (thisFQId == FQId) {
                        let thislog = JSON.parse(localStorage.getItem("Answer_" + e));
                        answerlogs.push(thislog);
                    }
                });
            }
        }
        return answerlogs;
    }

    /**
     * 删除单个作答记录
     * @param AnswerMenuid 
     */
    async delbaseAnswerlog(AnswerMenuid) {
        let thisAnswerid = AnswerMenuid.split(',')[0];
        if (this.db) {
            // 插入新的问卷信息
            const value = await this.db.executeSql(`delete  from QuesAnswer where answerId=?`, [thisAnswerid]);
        } else {
            let AnswerMenu = localStorage.getItem("AnswerMenu");
            let newAnswerMenu = "";
            //let newAnswerMenuarr= new Array<string>();
            if (AnswerMenu != undefined && AnswerMenu != "" && AnswerMenu != null) {
                let AnswerMenuArr = AnswerMenu.split('.');
                AnswerMenuArr.forEach(e => {
                    let temAnswerid = e.split(',')[0];
                    if (thisAnswerid != temAnswerid) {
                        newAnswerMenu += e + ".";
                    }
                    else {
                        //let thisAnswerlog=localStorage.getItem("Answer_"+e);
                        localStorage.removeItem("Answer_" + e);
                    }
                })
            }
            localStorage.setItem("AnswerMenu", newAnswerMenu);
        }
    }
    /**
     * 删除多个做大记录，作答人主键。
     * @param AnswerMenuidarr 
     */
    async  delbaseAnswerlogs(AnswerMenuidarr: Array<string>) {
        if (this.db) {
            let inWord = "'a'";
            AnswerMenuidarr.forEach(async AnswerMenuid => {
                await this.db.executeSql(`delete from QuesAnswer where answerId=?`, [AnswerMenuid.split(',')[0]]).catch(e => { console.log(`删除多个做大记录，作答人主键。`); console.log(e); });
            });
            // console.log(inWord);
            // const findAllResult = await this.db.executeSql(`select * from QuesAnswer `,{}).catch(e => { console.log(`删除多个做大记录，作答人主键。`); console.log(e); });
            // console.log(findAllResult);
            // // 插入新的问卷信息
            // for(let i = 0; i < AnswerMenuidarr.length; i ++){
            //  console.log(AnswerMenuidarr);
            //    const result  =   await this.db.executeSql(`delete from QuesAnswer where answerId=?`,AnswerMenuidarr[i]).catch(e => { console.log(`删除多个做大记录，作答人主键。`); console.log(e); });
            //    console.log(result);
            // }

        } else {
            let AnswerMenu = localStorage.getItem("AnswerMenu");
            let newAnswerMenu = "";

            //let newAnswerMenuarr= new Array<string>();
            if (AnswerMenu != undefined && AnswerMenu != "" && AnswerMenu != null) {

                let AnswerMenuArr = AnswerMenu.split('.');
                AnswerMenuidarr.forEach(AnswerMenuid => {
                    let thisAnswerid = AnswerMenuid.split(',')[0];
                    AnswerMenuArr.forEach((e, a) => {
                        let temAnswerid = e.split(',')[0];
                        if (thisAnswerid != temAnswerid) {
                            newAnswerMenu += e + "."
                        }
                        else {
                            //let thisAnswerlog=localStorage.getItem("Answer_"+e);
                            localStorage.removeItem("Answer_" + e);
                            AnswerMenuArr.splice(a, 1);

                        }
                    })
                });
                newAnswerMenu = AnswerMenuArr.join(".")
            }

            localStorage.setItem("AnswerMenu", newAnswerMenu);
        }
    }
    /**
     * 下载问卷
     * @param i 
     * @param list 
     * @param onQuestionnaire 
     */
    notdownstarta(i, list, onQuestionnaire) {//下载问卷
        // localStorage.setItem("localstorageSql" + i, JSON.stringify(list.array[i]));
        // onQuestionnaire.push(JSON.parse(localStorage.getItem("localstorageSql" + i)));
        // localStorage.setItem("localQuestion", JSON.stringify(onQuestionnaire));
        // onQuestionnaire=JSON.parse(localStorage.getItem("localQuestion"));
    }
    //改变作答状态
    /**
     * 改变作答状态
     * @param answerid 
     * @param FQId 
     * @param FPublishTime 
     * @param stateint 
     */
    async changeAnswerState(answerid, FQId, FPublishTime, stateint) {
        //
        let thisAnswerlog = await this.getbaseAnswerlogByanswerid(answerid);
        thisAnswerlog.FUpdateloadState = 2;
        await this.setbaseAnswerlogGoon(answerid, FQId, FPublishTime, thisAnswerlog);
    }
    //清空已上传
    /**
     * 清空已上传
     * @param FQId 
     */
    async clearUpladedAnswerlog(FQId) {
        if (this.db) {
            // 插入新的问卷信息
            const value = await this.db.executeSql(`delete from QuesAnswer where FUpdateloadState=? and fqid=?`, ['2', FQId]);
        } else {
            let answerlogs = await this.getbaseAnswerlogByFQId(FQId);

            answerlogs.forEach(onea => {
                if (onea.FUpdateloadState == 2) {
                    this.delbaseAnswerlog(onea.FAnswerId + "," + FQId + "," + onea.FPublishTime);
                }

            });
        }
    }
    //获取getUserId
    getUserId() {
        let userid = sessionStorage.getItem("userid");
        if (userid == undefined || userid == "" || userid == null) {
            userid = "";
        }
        return userid;
    }
    //获取轨迹
    /**
     *获取轨迹
     *
     * @param {*} FQId
     * @param {*} starttime
     * @param {*} endtime
     * @returns
     * @memberof localbase
     */
    async getGpsarr(FQId, starttime, endtime) {

        let Gpsarr = new Array<string>();
        if (this.db) {
            const value = await this.db.executeSql(`select body  from QuesAnswer where fqid=?`, [FQId]);
            for (let i = 0; i < value.rows.length; i++) {
                const e = value.rows.item(i)['body'];
                let thislog = JSON.parse(e);
                let thisgps = thislog.Gps;
                let publishtime = new Date(thislog.FPublishTime);
                //
                if (starttime == "" && endtime == "") {
                    Gpsarr.push(thislog.Gps);
                } else if (starttime == "" && endtime != "") {
                    let endtimetime = new Date(endtime);
                    if (endtimetime >= publishtime) {
                        Gpsarr.push(thislog.Gps);
                    }
                }
                else if (starttime != "" && endtime == "") {
                    let starttimetime = new Date(starttime);
                    if (starttime <= publishtime) {
                        Gpsarr.push(thislog.Gps);
                    }
                }
                else if (starttime != "" && endtime != "") {
                    let starttimetime = new Date(starttime);
                    let endtimetime = new Date(endtime);
                    if ((endtimetime >= publishtime) && (starttimetime <= publishtime)) {
                        Gpsarr.push(thislog.Gps);
                    }
                }
            }
        } else {
            let AnswerMenu = localStorage.getItem("AnswerMenu");
            if (AnswerMenu != undefined && AnswerMenu != "" && AnswerMenu != null) {
                let AnswerMenuArr = AnswerMenu.split('.');
                AnswerMenuArr.forEach(e => {
                    let thisFQId = e.split(',')[1];
                    if (thisFQId == FQId) {
                        let thislog = JSON.parse(localStorage.getItem("Answer_" + e));
                        let thisgps = thislog.Gps;
                        let publishtime = new Date(thislog.FPublishTime);
                        //
                        if (starttime == "" && endtime == "") {
                            Gpsarr.push(thislog.Gps);
                        } else if (starttime == "" && endtime != "") {
                            let endtimetime = new Date(endtime);
                            if (endtimetime >= publishtime) {
                                Gpsarr.push(thislog.Gps);
                            }
                        }
                        else if (starttime != "" && endtime == "") {
                            let starttimetime = new Date(starttime);
                            if (starttime <= publishtime) {
                                Gpsarr.push(thislog.Gps);
                            }
                        }
                        else if (starttime != "" && endtime != "") {
                            let starttimetime = new Date(starttime);
                            let endtimetime = new Date(endtime);
                            if ((endtimetime >= publishtime) && (starttimetime <= publishtime)) {
                                Gpsarr.push(thislog.Gps);
                            }
                        }
                    }
                });
            }
        }

        return Gpsarr;
    }


    //问卷作答分数加一
    /**
     *作答分数增加1
     *
     * @param {*} FQId
     * @param {*} FPublishtime
     * @memberof localbase
     */
    async QueNumPlusOne(FQId, FPublishtime) {

        let thisQue;
        thisQue = await this.getQueByFqid(FQId);

        if (thisQue.Que.answerlognumber == null) {
            thisQue.Que.answerlognumber = 0;
        }
        thisQue.Que.answerlognumber += 1;
        if (this.db) {
            await this.db.executeSql(`
            update  Ques set body=? where fqid=?`, [JSON.stringify(thisQue), FQId]).catch(e => { console.log(`QueNumPlusOne--update  Ques set body`); console.log(e); });
        }
        else {
            localStorage.setItem("QueLast_" + FQId + "," + FPublishtime, JSON.stringify(thisQue));
        }

    }
    //获取问卷分数
    async  getQueNum(FQId) {
        let thisQue;
        thisQue = await this.getQueByFqid(FQId).catch(e => { console.log(`getQueNum获取问卷分数`); console.log(e); });
        if (thisQue == null) {
            return 0;
        }
        else {
            return thisQue.Que.answerlognumber;
        }
    }
    //保存删除已结束问卷
    saveOverQue(FQId) {
        let delQue = localStorage.getItem("delQue");
        if (delQue == undefined || delQue == "" || delQue == null) {
            delQue = FQId;
        }
        else {
            delQue += "," + FQId;
        }
        localStorage.setItem("delQue", delQue);
    }
    //设置自动上传手动上传
    setAutoSc(isAuto) {
        if (isAuto == "Auto") {
            localStorage.setItem("AutoSc", isAuto);
        }
        else if (isAuto == "noAuto") {
            localStorage.setItem("AutoSc", isAuto);
        }
    }
    //取自动上传手动上传
    getAutoSc() {
        let AutoSc = localStorage.getItem("AutoSc");
        return AutoSc;
    }
    //已结束问卷是否删除
    isDelQue(FQId) {
        // localStorage.setItem( "delQue","");
        let result = false;
        let delQue = localStorage.getItem("delQue");
        if (delQue != undefined && delQue != "" && delQue != null) {
            let delQuearr = delQue.split(',');
            delQuearr.forEach(oned => {
                if (oned == FQId) {
                    result = true;
                }
            });
        }
        return result;
    }

}
